package com.zbf.设计模式1.代理模式1.动态代理1.CGLIB动态代理;

import com.alibaba.fastjson.JSON;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/23  9:40
 * 描述:
 */
public class My12MthodInterceptor<T> implements MethodInterceptor {

    private Enhancer enhancer=new Enhancer();

    private Object object;

    public My12MthodInterceptor(Object object) {
        this.object = object;
    }

    public  T getProxyObject(Class<T> tClass){

        enhancer.setSuperclass(tClass);

        enhancer.setCallback(this);

        return  (T)enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("====start===日志======");

        methodProxy.invoke(object,objects);

        System.out.println("====end===日志======");

        return JSON.toJSONString(objects);
    }


}
